﻿Public Class FViewSharedItemDetails_Remote
    Private Shared mActiveForms As New Generic.Dictionary(Of String, FViewSharedItemDetails_Remote)

    Public Shared Shadows Sub Show(ByVal owner As IWin32Window, ByVal node As Framework.Net.Node, ByVal item As Engine.Commands.GetShareItems.ItemInfo)
        Dim strKey As String = node.Buddy.Id.ToString & "_" & item.Id.ToString
        If mActiveForms.ContainsKey(strKey) Then
            mActiveForms(strKey).Activate()
        Else
            Dim clsView As New FViewSharedItemDetails_Remote
            clsView.ShowIt(strKey, node, item)
            mActiveForms.Add(strKey, clsView)
        End If
    End Sub

    Public Shared Sub CloseAll()
        For i As Integer = mActiveForms.Count - 1 To 0 Step -1
            mActiveForms.Values(i).Close()
        Next
    End Sub

    Private mSharedItem As Engine.Commands.GetShareItems.ItemInfo
    Private mNode As Framework.Net.Node
    Private mFormKey As String
    Private mWaitingForMessageIdEx As Integer

    Private Sub ShowIt(ByVal formKey As String, ByVal node As Framework.Net.Node, ByVal item As Engine.Commands.GetShareItems.ItemInfo)
        mFormKey = formKey
        mSharedItem = item
        mNode = node

        Me.Text = String.Format("{0} - {1} - Shared Item Details", item.Name, node.Buddy.NameLocal)
        Select Case item.Type
            Case Entities.ItemType.Movies
                Me.Icon = My.Resources.Movie
            Case Entities.ItemType.TVSeries
                Me.Icon = My.Resources.TV
            Case Else
                Me.Icon = Framework.My.Resources.Buddy
        End Select

        SharedItemMediaInfo1.UpdateView(Nothing, Nothing, Nothing)
        StartGetDetails(item)

        SetupEvents(True)
        MyBase.Show()
    End Sub

    Private Sub StartGetDetails(ByVal item As Engine.Commands.GetShareItems.ItemInfo)
        SharedItemMediaInfo1.Enabled = False
        mWaitingForMessageIdEx = Engine.Commands.GetSharedItemDetails.SendRequest(mNode, item.Id, AddressOf GetSharedItemDetailsReplyCallback, True)
    End Sub

    Private Sub FViewSharedItems_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mActiveForms.Remove(mFormKey)
        SetupEvents(False)
    End Sub
    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
        Else
        End If
    End Sub

    Private Sub GetSharedItemDetailsReplyCallback(ByVal inReplyToMessageId As Integer, ByVal node As Framework.Net.Node, ByVal reply As FileShare.Engine.Commands.GetSharedItemDetails.Reply)
        Try
            If inReplyToMessageId = mWaitingForMessageIdEx Then
                SharedItemMediaInfo1.Enabled = True
                SharedItemMediaInfo1.UpdateView(reply.Item, reply.Files, reply.MediaInfo)
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub btnDownloadAndClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownloadAndClose.Click
        Try
            If Manager.StartDownload(Me, mNode, mSharedItem) Then Me.Close()
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub btnIMDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIMDB.Click
        Try
            Dim uri As Uri = CType(btnIMDB.Tag, Uri)
            System.Diagnostics.Process.Start(uri.ToString)
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub SharedItemMediaInfo1_SetImdbLink(ByVal link As System.Uri) Handles SharedItemMediaInfo1.SetImdbLink
        btnIMDB.Visible = True
        btnIMDB.Tag = link
        btnIMDB.Text = Nothing
        btnIMDB.Image = Framework.My.Resources.IMDB_small
    End Sub
End Class